package com.Gientech.springbucks.service.serviceImpl;

import com.Gientech.springbucks.config.RollbackException;
import com.Gientech.springbucks.domain.dao.CoffeeQueryDto;
import com.Gientech.springbucks.domain.eo.Coffee;
import com.Gientech.springbucks.mapper.CoffeeMapper;
import com.Gientech.springbucks.service.CoffeeService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@CacheConfig(cacheNames = "coffee")
@Slf4j
public class CoffeeServiceImpl implements CoffeeService {

    @Autowired
    private CoffeeMapper coffeeMapper;

    @Cacheable
    public List<Coffee> findAllCoffee() {
        return coffeeMapper.findAll();
    }

    @CacheEvict
    public void reloadCoffee() {
    }

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    @Transactional
    public void insertRecord() {
        jdbcTemplate.execute("INSERT INTO FOO (BAR) VALUES ('AAA')");
    }

    @Override
    @Transactional(rollbackFor = RollbackException.class)
    public void insertThenRollback() throws RollbackException {
        jdbcTemplate.execute("INSERT INTO FOO (BAR) VALUES ('BBB')");
        throw new RollbackException();
    }

    @Override
    public void invokeInsertThenRollback() throws RollbackException {
//        insertThenRollback();
    }

    @Override
    public PageInfo<Coffee> queryBy(CoffeeQueryDto coffeeQueryDto) {
        return null;
    }

    @Override
    public void updateCoffee(Coffee coffee) {

    }

    @Override
    public void deleteCoffee(Coffee coffee) {

    }
}
